program fix_iza_sza

! Program to interchange iza and sza variables. The origin of this problem comes
! from an error reading profile files.
    
    use iasing_osse
    use netcdf

    implicit none

    character(len=256) :: carg
    
    integer :: ncid, nobs, nlevels
    type(r4a_ids) :: db_ids
    real(JPR4), dimension(:), allocatable :: iza, sza

    include 'checknc.interface'
    include 'r4a_initializer.interface'

    call get_command_argument(1,carg)
    if (len_trim(carg).eq.0) stop -4
    print*, trim(carg)
    print*

    call checknc(nf90_open(trim(carg), NF90_WRITE, ncid))
    db_ids = r4a_initializer(ncid)

    call checknc(nf90_inquire_dimension(ncid, db_ids%atm_did, len=nobs))
    print*, nobs
    allocate(iza(nobs))
    allocate(sza(nobs))

    call checknc(nf90_get_var(db_ids%opa%gid, db_ids%opa%iza, iza))
    call checknc(nf90_get_var(db_ids%opa%gid, db_ids%opa%sza, sza))

    if (any(iza.gt.90.)) then 
        print*, "need to interchange iza and sza"
        call checknc(nf90_put_var(db_ids%opa%gid, db_ids%opa%iza, sza))
        call checknc(nf90_put_var(db_ids%opa%gid, db_ids%opa%sza, iza))
    else
        print*, "everything it is ok"
    endif

    print'(10F8.3)', iza(1:40)
    print*
    print'(10F8.3)', sza(1:40)


    call checknc(nf90_inquire_dimension(ncid, db_ids%lev_did, len=nlevels))
    if (nlevels.eq.60) then
        call checknc(nf90_put_var(db_ids%dim%gid, db_ids%dim%lev, L60FN))
    endif

 
    
    call checknc(nf90_close(ncid))

end program

